package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    StudentService studentService = new StudentService();
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        String method = req.getParameter("method");
        System.out.println(method+"===============================================================================================");
        if ("selectByPage".equals(method)) {
            selectByPage(req,resp);
        }
        if ("addStu".equals(method)) {
            addStu(req,resp);
        }
        if ("update".equals(method)) {
            update(req,resp);
        }
        if ("deleteStu".equals(method)) {
            deleteStu(req,resp);
        }

    }

    private void deleteStu(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        studentService.deleteStu(number);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> parameterMap = req.getParameterMap(); //包含分页 和 student的元素
        Student student = new Student();
        try {
            BeanUtils.populate(student,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        studentService.update(student);
    }

    private void addStu(HttpServletRequest req, HttpServletResponse resp) {
//        String number = req.getParameter("number");
//        String name = req.getParameter("name");
//        String birthday = req.getParameter("birthday");
//        String address = req.getParameter("address");
        Map<String, String[]> parameterMap = req.getParameterMap(); //包含分页 和 student的元素
        Student student = new Student();
        try {
            BeanUtils.populate(student,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        studentService.addStu(student);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        List<Student> students = studentService.selectByPage(currentPage, pageSize);
        PageInfo pageInfo = new PageInfo<>(students);
        String s = null;

        try {
            s = new ObjectMapper().writeValueAsString(pageInfo);
            resp.getWriter().write(s);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
